{{extend name="layout/base-html"}}

{{block name="body"}}
<style>
    .xm-form-select {
        width: 100%;
    }

    .xm-select .scroll-body {
        padding-left: 10px;
        overflow: hidden;
    }

    xm-select > .xm-body .xm-body-custom * {
        box-sizing: border-box;
    }
</style>

<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <label for="username" class="layui-form-label"><span class="x-red">*</span>名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="username" name="username" required="" lay-verify="required"
                           autocomplete="off" class="layui-input" value="{{$data.username|default=''}}">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>将会成为您登录方式之一
                </div>
            </div>
            <div class="layui-form-item">
                <label for="mobile" class="layui-form-label"><span class="x-red">*</span>手机</label>
                <div class="layui-input-inline">
                    <input type="text" id="mobile" name="mobile" required="" lay-verify="mobile"
                           autocomplete="off" class="layui-input" value="{{$data.mobile|default=''}}">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>将会成为您登录方式之一
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_email" class="layui-form-label"><span class="x-red">*</span>邮箱</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_email" name="email" required="" lay-verify="email"
                           autocomplete="off" class="layui-input" value="{{$data.email|default=''}}">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>将会成为您登录方式之一
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label"><span class="x-red">*</span>密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="L_pass" name="password" {{if empty($data)}} required="" lay-verify="pass"
                           {{/if}}
                    autocomplete="off" class="layui-input" {{$data ? 'placeholder="空则不修改密码"' : ''}}}>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>8到16个字符
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label"><span class="x-red">*</span>确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="L_repass" name="renew_password" {{if empty($data)}} required=""
                           lay-verify="repass" {{/if}}
                    autocomplete="off" class="layui-input" {{$data ? 'placeholder="空则不修改密码"' : ''}}>
                </div>
            </div>

            {{if(!$data || $data.id != 1)}}
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>选择角色</label>

                <div class="layui-input-block">
                    <div id="roles" class="xm-form-select"></div>
                </div>
            </div>
            {{/if}}

            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label">
                </label>

                <button class="layui-btn" lay-filter="{{$data ? 'edit' : 'add'}}" lay-submit="">
                    {{$data ? '修改' : '增加'}}
                </button>
            </div>
        </form>
    </div>
</div>

<script type="module">
    import {update, save} from "/static/admin/js/api/additional/admin-user.js";
    import {rain} from "/static/admin/js/app.js";

    layui.use(['form', 'layer', 'transfer'], () => {
        let $ = layui.jquery,
            form = layui.form;

        //自定义验证规则
        form.verify({
            nikename: function (value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            },
            pass: [/(.+){8,16}$/, '密码必须8到16位'],
            repass: function (value) {
                if ($('#L_pass').val().trim() != value.trim()) return '两次密码不一致';
            }
        });

        let event = '{{$data ? "edit" : "add"}}';

        // 监听事件
        formData(form, event, 'edit' === event ? {
            request: update,
            id: '{{$data.id|default=0}}',
        } : {
            request: save,
        });

        if ('1' === '{{if !$data || $data.id !== 1}}1{{else}}0{{/if}}') {
            // 穿梭框
            (() => {
                let transfer = layui.transfer;

                // 权限列表
                let list = [];
                JSON.parse('{{$roles|raw}}').forEach(item => {
                    list.push({value: item.id, title: item.name});
                });

                // 编辑选中的权限
                let initValue = '{{$data.roleIds|raw|default=""}}'.split(',');

                // 渲染
                let rulesSelect = xmSelect.render({
                    ...rain.config.xmSelect,

                    el: '#roles',
                    content: "<div id='transfer'></div>",
                    height: 'auto',
                    direction: 'down',
                    autoRow: true,
                    initValue: initValue,
                    data: list,
                    name: 'roles',
                    prop: {name: 'title'},
                    layVerify: 'required',
                    on: function (data) {
                        if (!data.isAdd) {
                            transfer.reload('transfer', {
                                value: rulesSelect.getValue('value')
                            })
                        }
                    }
                });

                // 使用穿梭框组件
                transfer.render({
                    id: 'transfer',
                    elem: '#transfer',  //绑定元素
                    title: ['未选角色', '已选角色'],
                    data: list,
                    value: initValue,
                    width: "40%",
                    onchange: function (data, index) {
                        if (index == 0) {
                            rulesSelect.append(data)
                        } else {
                            rulesSelect.delete(data)
                        }
                    }
                });
            })();
        }
    });
</script>
{{/block}}
